﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestPhone
{
    public class SDCardFileStream : System.IO.Stream
    {
        public SDCardFileStream(System.IO.Stream source)
        {
            BaseStream = source;
        }

        public System.IO.Stream BaseStream { get; private set; }

        public override bool CanRead
        {
            get { return BaseStream.CanRead; }
        }

        public override bool CanSeek
        {
            get { return BaseStream.CanSeek; }
        }

        public override bool CanWrite
        {
            get { return false; }
        }

        public override void Flush()
        {
            BaseStream.Flush();
        }

        public override long Length
        {
            get { return BaseStream.Length; }
        }

        protected virtual long ChangeBugLong(long src)
        {
            byte[] b1 = BitConverter.GetBytes(src);
            byte[] b2 = new byte[8];
            Buffer.BlockCopy(b1, 0, b2, 4, 4);
            Buffer.BlockCopy(b1, 4, b2, 0, 4);
            return BitConverter.ToInt64(b2, 0);
        }

        public override long Position
        {
            get
            {
                return BaseStream.Position;
               // return ChangeBugLong(BaseStream.Position);
            }
            set
            {
                BaseStream.Position = ChangeBugLong(value);
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return BaseStream.Read(buffer, offset, count);
        }

        public override long Seek(long offset, System.IO.SeekOrigin origin)
        {
            return BaseStream.Seek(ChangeBugLong(offset), origin);
        }

        public override void SetLength(long value)
        {
            throw new NotSupportedException();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException();
        }
    }
}
